package BasicKnowledge.Basic.IO;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @time: 2021/2/2/002 15:16
 * @author: Zhurunhe
 */
public class Demo6 {
    public static void main(String[] args) {
        String path="G:/abc.txt";
        String topath="G:/bcd.txt";
        copyFile2(path,topath);
    }
    private static void copyFile1(String path, String topath){
        try(
                FileInputStream fileInputStream = new FileInputStream(path);
                FileOutputStream fileOutputStream = new FileOutputStream(topath);
                )
        {
            byte[] bytes=new byte[1024];//1024的偶数倍
            int len = fileInputStream.read(bytes);
            while (len!=-1){
                fileOutputStream.write(bytes,0,len);
                len = fileInputStream.read(bytes);
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    private static void copyFile2(String path, String topath) {
        FileInputStream fileInputStream= null;
        FileOutputStream fileOutputStream= null;
        try {
            fileInputStream = new FileInputStream(path);
            fileOutputStream = new FileOutputStream(topath);

            int fr=fileInputStream.read();
            while (fr!=-1) {
                fileOutputStream.write(fr);
                fr=fileInputStream.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fileInputStream!=null ){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }
}
